package com.lxs.votesystem.controller;

import com.lxs.votesystem.bean.VoteOption;
import com.lxs.votesystem.bean.VoteResult;
import com.lxs.votesystem.service.VoteOptionService;
import com.lxs.votesystem.util.DoVoteBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lxs.votesystem.service.VoteService;
import com.lxs.votesystem.util.AddVoteFormBean;
import com.lxs.votesystem.util.AjaxResult;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 投票控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/vote")
public class VoteController {

	@Autowired
	private VoteService voteService;
	@Autowired
	private VoteOptionService voteOptionService;

	/**
	 * 跳转到添加投票页面
	 * @return
	 */
	/*@RequestMapping("/toAddVote")
	public String toAddVote() {
		return "manage/addVote";
	}*/
	
	/**
	 * 跳转到显示所有投票页面,只跳转,不携带数据
	 * @return
	 */
	/*@RequestMapping("/toShowVote")
	public String toShowVote() {
		return "manage/showVote";
	}*/

	/**
	 * 跳转到投票页面
	 * @return
	 */
	@RequestMapping("/toDoVote")
	public String toDoVote(){
		return "potal/doVote";
	}
	
	/**
	 * 增加投票
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addVote")
	public AjaxResult addVote(AddVoteFormBean addVoteFormBean) {
		// 调用service层,执行操作
		try {
			voteService.addVote(addVoteFormBean);
			return new AjaxResult(true, "成功", null);
		} catch (Exception e) {
			return new AjaxResult(false, "失败", null);
		}
	}

	/**
	 * 异步加载所有投票选项
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/showVote")
	public AjaxResult showVote(){
		try {
			// 调用service层
			List<VoteResult> voteResults = voteService.queryAllVote();
			// 将查询到的数据放到AjaxResult对象中,返回
			return new AjaxResult(true,"成功",voteResults);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false,"失败",null);
		}
	}

	/**
	 * 异步删除投票
	 * @param voteID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delVote")
	public AjaxResult delVote(Integer voteID){
		// 调用server层,执行删除
		try {
			voteService.delVote(voteID);
			return new AjaxResult(true, "成功", null);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false, "失败", null);
		}
	}

    /**
     * 根据频道id,获取所有投票
     * @param channelID
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryVoteResultsByCID")
	public AjaxResult queryVoteResultsByCID(Integer channelID) {
        try {
            // 调用service层
            List<VoteResult> voteResults = voteService.queryVoteResultsByCID(channelID);
            // 将查询到的数据放到AjaxResult对象中,返回
            return new AjaxResult(true,"成功",voteResults);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"失败",null);
        }
    }

	/**
	 * 投票
	 * @param doVoteBean
	 * @param request
	 * @return
	 */
    @ResponseBody
	@RequestMapping("/doVote")
    public AjaxResult doVote(DoVoteBean doVoteBean, HttpServletRequest request, HttpServletResponse response) {
    	AjaxResult result = new AjaxResult();
    	// 判断当前电脑今天是否对这个投票选项进行投票
		// 获得cookies
		Cookie[] cookies = request.getCookies();
		// 判断cookie是否存在
		if (cookies != null && cookies.length > 0) {
			// 判断当期投票的id是否在cookie中
			for (Cookie cookie : cookies) {
				if (cookie.getValue().equals("voteID" + doVoteBean.getVoteID())) {
					// 今天投过票了
					return new AjaxResult(false, "你今天已经投过票了,请明天再来", null);
				}
			}
		}
		// 走到这,说明用户今天没有对当前的投票项进行投票
		// 判断用户是否选择了其他的投票选项
		if (doVoteBean.getVoteOptionID() == 0) {
			// 选择的是其他投票选项
			// 调用service层,将自定义的投票选项添加到数据库,并将票数设置为1
			// 封装VoteOption
			VoteOption voteOption = new VoteOption();
			voteOption.setTicketNum(1);
			voteOption.setVoteID(doVoteBean.getVoteID());
			voteOption.setVoteOptionName(doVoteBean.getOtherOption());
			try {
				voteOptionService.addVoteOption(voteOption);
				/*// 将当前的投票id放入cookie中,并设置过期时间
				Cookie cookie = new Cookie("voteID" + doVoteBean.getVoteID(), "voteID" + doVoteBean.getVoteID());
				// 获取系统当前时间距离今天结束还有多少时间
				// 获取系统当前时间距离今天结束还有多少时间
				Date endDate = new Date();
				endDate.setHours(24);
				endDate.setMinutes(59);
				endDate.setSeconds(59);
				Long distanceTime = (endDate.getTime() - new Date().getTime()) / 1000;
				cookie.setMaxAge(distanceTime.intValue());
				// 将cookie写入客户端
				response.addCookie(cookie);
				result.setSuccess(true);
				result.setMessage("投票成功");
				return new AjaxResult(true,"投票成功", null);*/
			} catch (Exception e) {
				e.printStackTrace();
				return new AjaxResult(false,"投票失败", null);
			}
		} else {
			// 用户所投的选项是已经存在的,将所投选项的票数加一即可
			try {
				voteService.doVote(doVoteBean.getVoteOptionID());
			} catch (Exception e) {
				e.printStackTrace();
				return new AjaxResult(false,"投票失败", null);
			}
		}

		// 将当前的投票id放入cookie中,并设置过期时间
		Cookie cookie = new Cookie("voteID" + doVoteBean.getVoteID(), "voteID" + doVoteBean.getVoteID());
		// 获取系统当前时间距离今天结束还有多少时间
		// 获取系统当前时间距离今天结束还有多少时间
		Date endDate = new Date();
		endDate.setHours(24);
		endDate.setMinutes(59);
		endDate.setSeconds(59);
		Long distanceTime = (endDate.getTime() - new Date().getTime()) / 1000;
		cookie.setMaxAge(distanceTime.intValue());
		// 将cookie写入客户端
		response.addCookie(cookie);
		result.setSuccess(true);
		result.setMessage("投票成功");
		return result;
	}

	/**
	 * 根据voteid,返回voteresult
	 * @param voteID
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getVoteResultByVoteID")
	public AjaxResult getVoteResultByVoteID(Integer voteID) {
		try {
			// 调用server层
			VoteResult voteResult = voteService.getVoteResultByVoteID(voteID);
			return new AjaxResult(true, "成功", voteResult);
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResult(false, "失败", null);
		}
	}
}
